package com.feifan.quartz.task;

import com.feifan.common.utils.send.SendMail;
import com.feifan.common.utils.send.SendSms;
import com.feifan.oa.service.WebSocketServer;
import com.feifan.quartz.domain.SysJob;
import com.feifan.quartz.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.HashMap;

/**
 * 日程提醒定时任务
 * @author: 非凡
 * @create: 2020-07-24 04:28
 **/
@Component("reminders")
public class Schedule {

    @Autowired
    private SendMail sendMail;

    @Autowired
    private ISysJobService sysJobService;

    @Autowired
    private SendSms sendSms;

    /**
     * 日程提醒
     * @param jobId       定时任务id
     * @param userName    用户姓名
     * @param toUser      用户的推送地址
     * @param title       用户代办事件名称
     * @param start       开始时间
     * @param end         结束时间
     * @param advanceTime 提前多少分钟
     * @param type        提醒方式
     * @throws ParseException
     */
    public void remind(String jobId, String userId, String userName, String toUser, String title, String start,
                       String end,
                       String advanceTime, String type) throws Exception {
        System.out.println("定时任务id = " + jobId);
        System.out.println("用户姓名 = " + userName);
        System.out.println("推送地址 = " + toUser);
        System.out.println("事件名称 = " + title);
        System.out.println("事件开始时间 = " + start);
        System.out.println("事件结束时间 = " + end);
        System.out.println("提前提醒分钟 = " + advanceTime);
        System.out.println("提醒类型 = " + type);

        /**
         * type
         * 0:不不提醒
         * 1:站内提醒
         * 2.邮箱提醒
         * 3.手机提醒
         */
        if ("1".equals(type)) {
            //站内提醒
            WebSocketServer.sendInfo("`/`?`尊敬的" + userName + ",你有一个代办事件即将开始<br/><br/>事件：" + title + "<br/><br" +
                    "/>事件办理时间：<br" +
                    "/>" + start + "<br/>" + end, userId);

        } else if ("2".equals(type)) {
            //邮箱提醒
            String html = "<h3>尊敬的&nbsp;" + userName + "&nbsp;你有一个代办事件即将开始&nbsp;</h3>" +
                    "<h3>日程名称:  " + title + "</h3><br>" +
                    "<h3>日程开始时间:  " + start + "</h3><br>" +
                    "<h3>日程结束时间:  " + end + "</h3><br>";
            sendMail.sendHtmlMail(toUser, "日程提醒", html);
        } else if ("3".equals(type)) {
            //短信提醒

            //发送的参数
            HashMap<Object, Object> param = new HashMap<>();
            param.put("code", jobId);

            //提交短信
            Boolean isSend = sendSms.send(toUser, "SMS_197891297", param);
            System.out.println("发送短信执行结果 - " + isSend);
        }

        //定时任务执行后删除定时任务事件
        SysJob sysJob = new SysJob();
        sysJob.setJobId(Long.parseLong(jobId));
        sysJob.setJobGroup("SCHEDULE");
        sysJobService.deleteJob(sysJob);


    }

}
